const Db = require('../services/mysql')
const { Fb, Moment, Op, Dev } = require('./utils')
const _ = require('lodash')

class Order {
    constructor() { }
 /**
   * 新增订单
   * @param {*} where
   */
  create(form) {
    return Db.Orders.create({
      orderid: 'ly' + Date.now(),
      pid: form.pid,
      ptype: form.ptype,
      total: form.total,
      userid: form.userid,
      orderstatus: '已付款',
      delstatus: '否',
      ptime: Moment.current(),
      num: form.num
    })
  }

  /**
   * 查找所有订单列表
   * @param {*} where
   */
  select(form, nowPage, pageSize) {
    let orderid =  form.orderid || '';
    let orderstatus = form.orderstatus || '';
    return Db.Orders.findAndCountAll({
      where: {
        orderid: {
          [Op.like]: '%' + orderid + '%'
        },
        orderstatus: {
          [Op.like]: '%' + orderstatus + '%'
        },
      },
      include: [Db.Sysuser, Db.Attractions],
      order: [
        ['ptime', 'DESC']
      ],
      offset: pageSize * (nowPage - 1),
      limit: pageSize
    })
  }

  /**
   * 查找单个用户订单列表
   * @param {*} where
   */
  selectOne(form, nowPage, pageSize) {
    let orderid =  form.orderid || '';
    return Db.Orders.findAndCountAll({
      where: {
        userid: form.id
      },
      include: [Db.Attractions],
      order: [
        ['ptime', 'DESC']
      ],
      offset: pageSize * (nowPage - 1),
      limit: pageSize
    })
  }

  /**
   * 新闻详情
   * @param {*} where
   */
  details(id) {
    return Db.Orders.findById(id)
  }

  /**
   * 订单退票
   * @param {*} where
   */
  update(from) {
    return Db.Orders.update(
      {
        orderstatus: '已退票',
      },
      {
        where: {
          id: from.id
        }
      }
    )
  }

  /**
   * 删除订单
   * @param {*} where
   */
  delete(from) {
    return Db.Orders.destroy({
      where: {
        id: from.id
      }
    })
  }
}

module.exports = new Order()